package main

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"house/api"
	"house/api/handler"
	"house/cmd/sub"
	"house/config"
	"house/consts"
	"house/inits"
	"house/internal/model"
	"house/internal/queue"
	"log"
)

func main() {
	config.Init()
	inits.MysqlInit()
	inits.InitRabbitMq()

	//房价调整
	go SendHousePrice()
	//聊天
	go sub.Sub()

	router := gin.Default()
	//文件上传
	router.Static("/upload", "D:\\gowork\\src\\house\\static\\upload\\")

	api.LoadRouters(router)

	router.Run(":9999") // 监听并在 0.0.0.0:9999 上启动服务
}

// 房价调整
func SendHousePrice() {
	inits.InitRabbitMq()

	msgs := queue.PublishSub(consts.UPDATE_HOUSE_PRICE)

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)

			var data model.House
			_ = json.Unmarshal(d.Body, &data)
			for _, conn := range handler.OnlineUser {
				handler.WSRespSuccess(conn, fmt.Sprintf("%s的房价调整为%d万", data.Title, data.Price/100000))
			}

		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
